package com.yylending.las.common.widgets;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.Style;
import android.graphics.RectF;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.View;

import com.yylending.las.R;
/**
 * 额度计算中圆弧
 * Created by laixj on 2017/6/8.
 * Copyright ©2016 www.yylending.com. All Rights Reserved.
 */
public class CircleLoading extends View {

	private static final String TAG = CircleLoading.class.getSimpleName();

	/**
	 * 圆半径
	 */
	private int mCircleRadius = 168;
	/**
	 * 圆的画笔宽度
	 */
	private int mCircleWidth = 18;
	/**
	 * 圆的画笔颜色
	 */
	private int mCircleColor = 0xddebfe;
	/**
	 * 圆弧画笔颜色
	 */
	private int mArcColor = 0x4b7cf9;
	/**
	 * 起始小圆点画笔颜色
	 */
	private int mPointColor = 0xFFFFFF;
	/**
	 * 圆心坐标
	 */
	private int mCircleX, mCircleY;
	private Paint mCirclePaint = new Paint();
	private Paint mArcPaint = new Paint();
	private Paint mPointPaint = new Paint();
	/**
	 * 圆弧边界
	 */
	private RectF mArcBounds;
	/**
	 * 圆弧开始角度
	 */
	private int mStartAngle = 270;
	/**
	 * 圆弧结束角度
	 */
	private int mSweepAngle = 0;
	/**
	 * 起始小圆点半径
	 */
	private int mPointRadius = 0;
	/**
	 * 是否需要递增动画
	 */
	private boolean mNeedAnimation = false;
	/**
	 * 递增延迟时间
	 */
	private int mAnimationDelay = 10;
	/**
	 * 递增延迟时间
	 */
	private int mIncreaseAngle = 30;
	/**
	 * 递增次数
	 */
	private int mEndPiece = 12;
	/**
	 * 圆分成多少份
	 */
	private int mPieceCount = 12;
	/**
	 * 递增计数
	 */
	private int mCount = 0;

	private boolean mShowCircleOnly = false;

	/**
	 * The constructor for the ProgressWheel
	 *
	 * @param context
	 * @param attrs
	 */
	public CircleLoading(Context context, AttributeSet attrs) {
		super(context, attrs);
		parseAttributes(context.obtainStyledAttributes(attrs, R.styleable.CircleLoading));
	}

	/**
	 * The constructor for the ProgressWheel
	 *
	 * @param context
	 */
	public CircleLoading(Context context) {
		super(context);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		int viewWidth = mCircleWidth + this.getPaddingLeft() + this.getPaddingRight();
		int viewHeight = mCircleWidth + this.getPaddingTop() + this.getPaddingBottom();
		int widthMode = MeasureSpec.getMode(widthMeasureSpec);
		int widthSize = MeasureSpec.getSize(widthMeasureSpec);
		int heightMode = MeasureSpec.getMode(heightMeasureSpec);
		int heightSize = MeasureSpec.getSize(heightMeasureSpec);

		int width;
		int height;

		if (widthMode == MeasureSpec.EXACTLY) {
			width = widthSize;
		} else if (widthMode == MeasureSpec.AT_MOST) {
			width = Math.min(viewWidth, widthSize);
		} else {
			width = viewWidth;
		}

		if (heightMode == MeasureSpec.EXACTLY || widthMode == MeasureSpec.EXACTLY) {
			height = heightSize;
		} else if (heightMode == MeasureSpec.AT_MOST) {
			height = Math.min(viewHeight, heightSize);
		} else {
			height = viewHeight;
		}
		setMeasuredDimension(width, height);
	}

	/**
	 * Use onSizeChanged instead of onAttachedToWindow to get the dimensions of
	 * the view, because this method is called after measuring the dimensions of
	 * MATCH_PARENT & WRAP_CONTENT. Use this dimensions to setup the bounds and
	 * paints.
	 */
	@Override
	protected void onSizeChanged(int w, int h, int oldw, int oldh) {
		super.onSizeChanged(w, h, oldw, oldh);
		setupBounds(w, h);
		setupPaints();
		invalidate();
	}

	/**
	 * Set the properties of the paints we're using to draw the progress wheel
	 */
	private void setupPaints() {
		mCirclePaint.setColor(mCircleColor);
		mCirclePaint.setAntiAlias(true);
		mCirclePaint.setStyle(Style.STROKE);
		mCirclePaint.setStrokeWidth(mCircleWidth);

		mArcPaint.setColor(mArcColor);
		mArcPaint.setAntiAlias(true);
		mArcPaint.setStyle(Style.STROKE);
		mArcPaint.setStrokeWidth(mCircleWidth);
		mArcPaint.setStrokeCap(Paint.Cap.ROUND);

		mPointPaint.setColor(mPointColor);
		mPointPaint.setAntiAlias(true);
		mPointPaint.setStyle(Style.FILL_AND_STROKE);
	}

	/**
	 * Set the bounds of the component
	 */
	private void setupBounds(int layout_width, int layout_height) {
		int paddingTop = getPaddingTop();
		int paddingBottom = getPaddingBottom();
		int paddingLeft = getPaddingLeft();
		int paddingRight = getPaddingRight();
		int minValue = Math.min(layout_width - paddingLeft - paddingRight, layout_height - paddingBottom - paddingTop);
		int circleDiameter = Math.min(minValue, mCircleRadius * 2 + mCircleWidth * 2);
		mCircleX = (layout_width - paddingLeft - paddingRight - circleDiameter) / 2 + paddingLeft + mCircleRadius + mCircleWidth;
		mCircleY = (layout_height - paddingTop - paddingBottom - circleDiameter) / 2 + paddingTop + mCircleRadius + mCircleWidth;
		mArcBounds = new RectF(mCircleX - mCircleRadius, mCircleY - mCircleRadius, mCircleX + mCircleRadius, mCircleY + mCircleRadius);
	}

	/**
	 * Parse the attributes passed to the view from the XML
	 *
	 * @param a the attributes to parse
	 */
	private void parseAttributes(TypedArray a) {
		DisplayMetrics metrics = getContext().getResources().getDisplayMetrics();
		mCircleRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mCircleRadius, metrics);
		mCircleWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mCircleWidth, metrics);
		mCircleRadius = (int) a.getDimension(R.styleable.CircleLoading_circle_radius, mCircleRadius);
		mCircleWidth = (int) a.getDimension(R.styleable.CircleLoading_circle_width, mCircleWidth);
		mCircleColor = a.getColor(R.styleable.CircleLoading_circle_color, mCircleColor);
		mArcColor = a.getColor(R.styleable.CircleLoading_arc_color, mArcColor);
		mStartAngle = a.getInt(R.styleable.CircleLoading_acr_startAngle, mStartAngle);
		mSweepAngle = a.getInt(R.styleable.CircleLoading_acr_sweepAngle, mSweepAngle);
		mPointColor = a.getColor(R.styleable.CircleLoading_point_color, mPointColor);
		mPointRadius = (int) a.getDimension(R.styleable.CircleLoading_point_radius, mPointRadius);
		mNeedAnimation = a.getBoolean(R.styleable.CircleLoading_increase_anim, mNeedAnimation);
		mAnimationDelay = a.getInt(R.styleable.CircleLoading_increase_anim_delay, mAnimationDelay);
		mIncreaseAngle = a.getInt(R.styleable.CircleLoading_increase_anim_angle, mIncreaseAngle);
		mPieceCount = a.getInt(R.styleable.CircleLoading_increase_piece_count, mPieceCount);
		mIncreaseAngle = 360 / mPieceCount;
		mEndPiece = a.getInt(R.styleable.CircleLoading_increase_end_piece, mEndPiece);
		a.recycle();
	}

	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		canvas.drawCircle(mCircleX, mCircleY, mCircleRadius, mCirclePaint);
		if (!mShowCircleOnly) {
			if (mNeedAnimation) {
				if (mCount < mEndPiece) {
					mSweepAngle = mSweepAngle + mIncreaseAngle;
					mCount++;
					canvas.drawArc(mArcBounds, mStartAngle, mSweepAngle, false, mArcPaint);
					postInvalidateDelayed(mAnimationDelay);
				} else {
					canvas.drawArc(mArcBounds, mStartAngle, mSweepAngle, false, mArcPaint);
				}
			} else {
				canvas.drawArc(mArcBounds, mStartAngle, mSweepAngle, false, mArcPaint);
			}


			if (mPointRadius > 0 && mEndPiece > 0) {
				if (mNeedAnimation) {
					if (mSweepAngle != 360) {
						canvas.drawCircle(mCircleX + (mCircleWidth - 2 * mPointRadius) / 2, mCircleY - mCircleRadius, mPointRadius, mPointPaint);
					}
				} else {
					canvas.drawCircle(mCircleX + (mCircleWidth - 2 * mPointRadius) / 2, mCircleY - mCircleRadius, mPointRadius, mPointPaint);
				}
			}
		}
	}

	@Override
	protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
		super.onVisibilityChanged(changedView, visibility);
	}

	public void setEndPiece(int endPiece){
		mEndPiece = endPiece;
	}

	public void setIncreaseAngle(int angle){
		mIncreaseAngle = angle;
	}

	public void setPieceCount(int pieceCount){
		mPieceCount = pieceCount;
		mIncreaseAngle = 360 / mPieceCount;
	}

	public void restartAnim(){
		mCount = 0;
		mSweepAngle = 0;
		postInvalidateDelayed(mAnimationDelay);
	}

	public void setAnimationDelay(int delay){
		mAnimationDelay = delay;
	}

	/**
	 *
	 * @param showCircleOnly true, 仅显示背景圆环(目前仅在还款已逾期时需要); false, 同时显示进度.
	 * @param circleColor  背景圆环颜色.
	 */
	public void showCircleOnly(boolean showCircleOnly, int circleColor){
		if (circleColor != -1) {
			mCirclePaint.setColor(circleColor);
		}
		mShowCircleOnly = showCircleOnly;
		postInvalidate();
	}
}